package zhtt.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import zhtt.bean.User01Service;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Created by zhaott on 2018/8/10.
 */
@WebListener
public class SessionListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        ApplicationContext ctx= WebApplicationContextUtils.getRequiredWebApplicationContext(event.getSession().getServletContext());
        User01Service userService=(User01Service)ctx.getBean("User01Service");
        String userInfo=userService.get();
        System.out.println(userInfo);
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        ApplicationContext ctx= WebApplicationContextUtils.getRequiredWebApplicationContext(event.getSession().getServletContext());
        User01Service userService=(User01Service)ctx.getBean("User01Service");
        String userInfo=userService.get();
        System.out.println(userInfo);
    }
}
